package com.xxq.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// // import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxq.common.valid.AddGroup;
import com.xxq.common.valid.DeleteGroup;
import com.xxq.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.xxq.gulimall.product.entity.AttrEntity;
import com.xxq.gulimall.product.entity.CategoryEntity;
import com.xxq.gulimall.product.service.AttrAttrgroupRelationService;
import com.xxq.gulimall.product.service.AttrService;
import com.xxq.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xxq.gulimall.product.entity.AttrGroupEntity;
import com.xxq.gulimall.product.service.AttrGroupService;
import com.xxq.common.utils.PageUtils;
import com.xxq.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author xxq
 * @email 1607903138@qq.com
 * @date 2022-04-28 20:20:14
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // // // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     *  03、 获取分类属性分组
     */
    @RequestMapping("/list/{catelogId}")
    // // // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryByCatelogId(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     *  04、 获取属性分组详情
     */
    @RequestMapping("/info/{attrGroupId}")
    // // // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getByIdWithCatelogPath(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     *  10、 获取属性分组的关联的所有属性
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R getGroupAttrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrGroup = attrGroupService.getRelation(attrGroupId);

        return R.ok().put("data", attrGroup);
    }

    /**
     *  17、 获取分类下所有分组&关联属性
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getGroupWithAttr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupEntity> attrGroup = attrGroupService.getGroupWithAttr(catelogId);

        return R.ok().put("data", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // // // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     *  11、 添加属性与分组关联关系
     */
    @RequestMapping("/attr/relation")
    // // // @RequiresPermissions("product:attrgroup:save")
    public R saveRelation(@Validated(AddGroup.class) @RequestBody List<AttrAttrgroupRelationEntity> entityList){
        attrGroupService.saveRelation(entityList);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // // // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // // // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     *  12、 删除属性与分组的关联关系
     */
    @RequestMapping("attr/relation/delete")
    // // // @RequiresPermissions("product:attrgroup:delete")
    public R deleteRelation(@Validated(DeleteGroup.class) @RequestBody List<AttrAttrgroupRelationEntity> entityList){
        attrGroupService.deleteRelation(entityList);

        return R.ok();
    }

    /**
     *  TODO 13、 获取属性分组没有关联的其他属性
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    // // // @RequiresPermissions("product:attrgroup:delete")
    public R getNoAttr(@RequestParam Map<String, Object> params,
                       @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);

        return R.ok().put("data", page);
    }

}
